<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

class files {

	var $id;
	var $name;
	var $path;
	var $comment;
	var $statusid;
	var $userid;
	var $teamid;
	var $lastupdate;
	var $subprj;
	var $stamp;
	
	var $prj;
	var $pre;
	var $url;
	
	/* STATS */
	var $translated;
	var $fuzzy;
	var $untranslated;
	var $filescount;
	
	var $order;
	var $auto;
	
    public function __construct($prj)
    {
    	$this->prj=PROJECT_PREFIX.$prj;
    	$this->pre=$prj;
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('files');	
		//return $class->newInstance();
		return new files($this->pre);
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".FILES.$this->prj." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function findFile() {
		$sql = "SELECT * FROM ".FILES.$this->prj." WHERE name = ".util::str($this->name). " AND teamid =".util::str($this->teamid);
		return $sql;
	}
	
	public function listAll() {
		$sql = "SELECT * FROM ".FILES.$this->prj;
		return $sql;
	}

	public function workloadForUser() {
		$sql = "SELECT userid, count(id) as count, sum(translated) as translated, sum(fuzzy) as fuzzy, sum(untranslated) as untranslated FROM ".FILES.$this->prj." WHERE userid=".util::str($this->userid)." AND (statusid=".util::str(FILE_STATUS_LOCKED)." OR statusid=".util::str(FILE_STATUS_PROOF).") AND teamid=".util::str($this->teamid)." GROUP BY userid";
		return $sql;
	}

	public function workload() {
		$sql = "SELECT userid, count(id) as count, sum(translated) as translated, sum(fuzzy) as fuzzy, sum(untranslated) as untranslated FROM ".FILES.$this->prj." WHERE (statusid=".util::str(FILE_STATUS_LOCKED)." OR statusid=".util::str(FILE_STATUS_PROOF).") AND teamid=".util::str($this->teamid)." GROUP BY userid";
		return $sql;
	}
	
	public function loadfiltered() {
		$sql = "SELECT * FROM ".FILES.$this->prj;
		$filtered = "";
		if ($this->statusid != null) {
			if (count($this->statusid)>1) {
				$filtered .= " (statusid = ".util::str($this->statusid[0]);
				for ($i=1;$i<count($this->statusid);$i++) {
					$filtered .= " OR statusid = ".util::str($this->statusid[$i]);
				}
				$filtered.=")";
			} else {
				$filtered .= " statusid = ".util::str($this->statusid);	
			}
		}
		if ($this->userid != null) {
			if ($filtered != "") $filtered .= " AND ";			
			$filtered .= " userid = ".util::str($this->userid);
		}
		if ($this->path != null) {
			if ($filtered != "") $filtered .= " AND ";
			$filtered .= " path = ".util::str($this->path);
		}
		if ($this->subprj != null) {
			if ($filtered != "") $filtered .= " AND ";
			$filtered .= " prjid = ".util::str($this->subprj);
		}
		if ($this->teamid != null) {
			if ($filtered != "") $filtered .= " AND ";
			$filtered .= " teamid = ".util::str($this->teamid);
		}		
		if ($filtered != "") $sql .= " WHERE " .$filtered;
		if (isset($this->order)) {
			$sql .= " ORDER BY name ".($this->order == 'asc' ? "ASC" : "DESC");
		}	
		return $sql;
	}
	
	public function countTeam() {
		$sql = "SELECT count(id) FROM ".FILES.$this->prj." WHERE teamid = ".util::str($this->teamid);
		return $sql;
	}
	
	public function pathlist() {
		$sql = "SELECT path FROM ".FILES.$this->prj." group by path";
		return $sql;
	}

	public function insert() {
      $sql = "INSERT INTO ".FILES.$this->prj." (name, path, statusid, teamid, prjid) ";
      $sql .= "VALUES (".util::str($this->name).",".util::str($this->path).",";
      $sql .= util::str(FILE_STATUS_TODO).",".util::str($this->teamid).",".util::str($this->subprj).")";
	  return $sql;
	}

	public function update() {
		$now = util::now();	
      $sql = "UPDATE ".FILES.$this->prj;
      $sql .= " set statusid = ".util::str($this->statusid);
      $sql .= ", lastupdate = ".util::str($now);
      $sql .= ", userid = ".util::str($this->userid);
      $sql .= ", comment = ".util::str(util::encode($this->comment));
      $sql .= ", chstamp = ".util::str($this->stamp);
      $sql .= ", autoflag = ".util::str($this->auto);
      $sql .= " WHERE id = ".util::str($this->id);
	  return $sql;
	}

	public function stats() {
      $sql = "UPDATE ".FILES.$this->prj;
      $sql .= " set translated = ".util::str($this->translated);
      $sql .= ", fuzzy = ".util::str($this->fuzzy);
      $sql .= ", untranslated = ".util::str($this->untranslated);
      $sql .= " WHERE id = ".util::str($this->id);
	  return $sql;
	}
	
	public function deleteTeam() {
		$sql = "DELETE FROM ".FILES.$this->prj;
		$sql .= " WHERE teamid = ".util::str($this->teamid);
		$sql .= " AND prjid = ".util::str($this->subprj);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".FILES.$this->prj;
		$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function state() {
		if ($this->untranslated=="" or $this->fuzzy=="") {
			return 0;
		} elseif ($this->untranslated>0) {
			return 3;
		} elseif ($this->fuzzy>0) {
			return 2;
		} else {
			return 1;
		}
	}		

	public function fill($array) {
		$this->id=$array['id'];
		$this->name=$array['name'];
		$this->path=$array['path'];
		$this->comment=util::decode($array['comment']);
		$this->statusid= $array['statusid'];
		$this->teamid=$array['teamid'];
		$this->userid=$array['userid'];
		$this->lastupdate=$array['lastupdate'];
		$this->subprj=$array['prjid'];
		$this->stamp=$array['chstamp'];
		$this->translated=$array['translated'];
		$this->fuzzy=$array['fuzzy'];
		$this->untranslated=$array['untranslated'];
		$this->auto=$array['autoflag'];
		$this->filescount=$array['count'];
	}

}

?>
